package com.panasonic.web.controller.cms;


import com.panasonic.common.annotation.Log;
import com.panasonic.common.core.controller.BaseController;
import com.panasonic.common.core.domain.AjaxResult;
import com.panasonic.common.core.page.TableDataInfo;
import com.panasonic.common.enums.BusinessType;
import com.panasonic.web.domain.cms.CmsArticle;
import com.panasonic.web.service.ICmsArticleService;
import com.panasonic.web.vo.CmsArticleMoveCategoryVo;
import com.panasonic.web.vo.CmsArticleTopChangeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章Controller
 *
 * @author lwf
 * @date 2021-11-12
 */
@RestController
@RequestMapping("/cms/article")
@Api(value = "CmsArticle", tags = "文章")
public class CmsArticleController extends BaseController {
    @Autowired
    private ICmsArticleService cmsArticleService;

    /**
     * 查询文章列表
     */
    @ApiOperation(value = "查询")
    @GetMapping("/list")
    public TableDataInfo list(CmsArticle cmsArticle) {
        startPage();
        List<CmsArticle> list = cmsArticleService.selectCmsArticleList(cmsArticle);
        return getDataTable(list);
    }


    /**
     * 获取文章详细信息
     */
    @ApiOperation(value = "获取文章详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(cmsArticleService.selectCmsArticleById(id));
    }

    /**
     * 新增文章
     */
    @ApiOperation(value = "新增文章")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsArticle cmsArticle) {
        return toAjax(cmsArticleService.insertCmsArticle(cmsArticle));
    }

    /**
     * 修改文章
     */
    @ApiOperation(value = "修改文章")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsArticle cmsArticle) {
        return toAjax(cmsArticleService.updateCmsArticle(cmsArticle));
    }

    /**
     * 删除文章
     */
    @ApiOperation(value = "删除文章")
    @Log(title = "文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cmsArticleService.deleteCmsArticleByIds(ids));
    }


    @ApiOperation(value = "修改文章类目")
    @Log(title = "修改文章类目", businessType = BusinessType.UPDATE)
    @PostMapping("/changeCategory")
    public AjaxResult changeCategory(@RequestBody CmsArticleMoveCategoryVo categoryVo) {

        return toAjax(cmsArticleService.changeCategory(categoryVo));
    }

    @ApiOperation(value = "修改定状态，0 置顶 1 取消置顶")
    @Log(title = "修改文章置顶", businessType = BusinessType.UPDATE)
    @PostMapping("/changeTop")
    public AjaxResult changeTop(@RequestBody @Validated CmsArticleTopChangeVo articleTopChange) {

        return toAjax(cmsArticleService.changeTop(articleTopChange));
    }
}
